package cn.djb.day3;

/**
 * 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如，输入字符串"abcdefg"和数字2，该函数将返回左旋转两位得到的结果"cdefgab"。
 */
class Solution2 {

    public String reverseLeftWords(String s, int n) {
        StringBuilder builder = new StringBuilder();
        StringBuilder last = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (i < n) {
                last.append(c);
            } else {
                builder.append(c);
            }
        }
        builder.append(last);
        return builder.toString();
    }

    public String reverseLeftWords2(String s, int n) {
        StringBuilder builder = new StringBuilder();

        for (int i = n; i < s.length(); i++) {
            char c = s.charAt(i);
            builder.append(c);
        }
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            builder.append(c);
        }
        return builder.toString();
    }

    /**
     * 求余算法
     *
     * @param s
     * @param n
     * @return
     */
    public String reverseLeftWords3(String s, int n) {
        StringBuilder builder = new StringBuilder();

        for (int i = n; i < n + s.length(); i++) {
            char c = s.charAt(i / s.length());
            builder.append(c);
        }

        return builder.toString();
    }
}